package com.jrx.anytxn.param.controller.system;

import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.param.entity.PrBankInstInfo;
import com.jrx.anytxn.param.entity.PrBankInstInfoVo;
import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import com.jrx.anytxn.param.service.system.IBankInstInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 分支行-机构信息表
 *
 * @author Liuzb
 * @date 2022/04/21
 */
@Api(tags = "分支行-机构信息API接口")
@RestController
@RequestMapping(value = "/param/bankInst")
public class BankInstInfoController {
    private static final Logger logger = LoggerFactory.getLogger(BankInstInfoController.class);

    @Autowired
    private IBankInstInfoService iBankInstInfoService;
    @Autowired
    private InfoService infoService;

    @ApiOperation(value = "查询支行所在区域的所有支行", notes = "分支行-机构信息表")
    @PostMapping(value = "/subBranchInfo")
    public TxnRespResult<List<PrBankInstInfo>> subBranchInfo(@RequestBody PrBankInstInfo bankInstInfo) {
        TxnRespResult<List<PrBankInstInfo>> result = new TxnRespResult<>();
        logger.info("get PrBankInstInfo start");
        try {
            // 根据机构表编号查询通区域的所有支行信息
            List<PrBankInstInfo> list = iBankInstInfoService.subBranchInfo(bankInstInfo);
            result.getSuccess(list);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        logger.info("get PrBankInstInfo end");
        return result;
    }

    @ApiOperation(value = "查询所有分行", notes = "分支行-机构信息表")
    @GetMapping(value = "/branchInfo")
    public TxnRespResult<List<PrBankInstInfo>> branchInfo() {
        TxnRespResult<List<PrBankInstInfo>> result = new TxnRespResult<>();
        logger.info("get PrBankInstInfo start");
        try {
            // 查询所有分行信息
            List<PrBankInstInfo> list = iBankInstInfoService.branchInfo();
            result.getSuccess(list);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        logger.info("get PrBankInstInfo end");
        return result;
    }

    @ApiOperation(value = "根据租户ID和主键ID查询", notes = "根据租户ID和主键ID查询")
    @GetMapping(value = "/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", required = true)
    })
    public TxnRespResult<PrBankInstInfo> getBankInstInfoById(@NotNull @PathVariable String id) {
        TxnRespResult<PrBankInstInfo> result = new TxnRespResult<>();
        PrBankInstInfo res;
        try {
            res = iBankInstInfoService.getBankInstInfoById(String.valueOf(infoService.getUser().getProjectId()), id);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "新增分支行-机构信息", notes = "新增分支行-机构信息")
    @PostMapping("/add")
    public TxnRespResult<PrBankInstInfo> addBankInstInfo(@RequestBody PrBankInstInfo bankInstInfo) {
        TxnRespResult<PrBankInstInfo> result = new TxnRespResult<>();
        PrBankInstInfo res;
        try {
            bankInstInfo.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            bankInstInfo.setUpdateBy(infoService.getUser().getUserName());
            res = iBankInstInfoService.addBankInstInfo(bankInstInfo);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "分页查询分支行-机构信息参数信息", notes = "分页查询分支行-机构信息参数信息")
    @PostMapping(value = "/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<PrBankInstInfo>> getAllPageByTenId(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize,
                                                                    @RequestBody PrBankInstInfo bankInstInfo) {
        TxnRespResult<TxnPage<PrBankInstInfo>> result = new TxnRespResult<>();
        TxnPage<PrBankInstInfo> response;
        try {
            bankInstInfo.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            response = iBankInstInfoService.findAllPageByTenantId(pageNum, pageSize, bankInstInfo);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "修改分支行-机构信息", notes = "修改分支行-机构信息")
    @PutMapping("/modify")
    public TxnRespResult<PrBankInstInfo> modifyPrBankInstInfo(@RequestBody PrBankInstInfo bankInstInfo) {
        TxnRespResult<PrBankInstInfo> result = new TxnRespResult<>();
        PrBankInstInfo res;
        try {
            bankInstInfo.setUpdateBy(infoService.getUser().getUserName());
            res = iBankInstInfoService.modifyPrBankInstInfo(bankInstInfo);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据ID删除信息", notes = "根据ID删除信息")
    @DeleteMapping(value = "/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "分支行-机构信息ID", required = true)
    })
    public TxnRespResult<Boolean> removeByTenIdAndId(@NotNull @PathVariable String id) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            DeleteLog deleteLog = new DeleteLog(infoService.getUser().getUserName());
            DeleteLogHelper.setDelleteHistory(deleteLog);

            flag = iBankInstInfoService.removeByTenantIdAndId(String.valueOf(infoService.getUser().getProjectId()), id);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }


    @ApiOperation(value = "根据条件查找分支行信息", notes = "根据条件查找分支行信息")
    @PostMapping(value = "/list")
    public TxnRespResult<List<PrBankInstInfo>> getInfoList(@RequestBody PrBankInstInfo bankInstInfo) {
        TxnRespResult<List<PrBankInstInfo>> result = new TxnRespResult<>();
        bankInstInfo.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
        List<PrBankInstInfo> bankInstInfoList = iBankInstInfoService.queryByPrBankInstInfo(bankInstInfo);
        return result.getSuccess(bankInstInfoList);
    }


    @ApiOperation(value = "查询上级分行", notes = "查询上级分行")
    @PostMapping(value = "/queryLeaderBank")
    public TxnRespResult<PrBankInstInfoVo> queryLeaderBank(@RequestBody PrBankInstInfo bankInstInfo) {
        TxnRespResult<PrBankInstInfoVo> result = new TxnRespResult<>();
        PrBankInstInfoVo prBankInstInfoVo = null;
        try {
            prBankInstInfoVo = iBankInstInfoService.queryLeaderBank(bankInstInfo);
        } catch (TxnException e) {
            return result.getFail(e.getErrCode(), e.getErrMsg());
        }
        return result.getSuccess(prBankInstInfoVo);
    }
}
